package chapter08;

import java.io.*;

public class Java06_IO_Object {
    public static void main(String[] args) {
        // TODO Java IO - 序列化 & 反序列化

        // TODO 数据文件对象
        File dataFile = new File("D:\\practiceCode\\java-top-speed\\data\\obj.bat");

        // TODO 对象输出流
        ObjectOutputStream objOut = null;
        FileOutputStream out = null;

        // TODO 对象输入流
        ObjectInputStream objIn = null;
        FileInputStream in = null;

        try{
            out = new FileOutputStream(dataFile);
            objOut = new ObjectOutputStream(out);

            // java中只有增加特殊标记的类，才能在写文件时进行序列化操作
            // 这里的标记其实是一个接口
            User user = new User();
            objOut.writeObject(user);
            objOut.flush();

            // TODO 从文件中读取数据转换成对象
            in = new FileInputStream(dataFile);
            objIn = new ObjectInputStream(in);

            Object o = objIn.readObject();
            System.out.println(o);  // chapter08.User@15327b79b

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (objOut != null) {
                try {
                    objOut.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}
class User implements Serializable {

}
